package com.wc.AlgoOJ_train.train2_DP训练.c7_状压DP.DP0601_摸鱼;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/21 19:07
 * @description
 */
public class Main {
    static int N = 22;
    // 满足状态的数
    static int[] state = new int[1 << N];
    // 不满足状态的数
    static int[] unState = new int[N];
    static int stateLen = 0;
    static int unStateLen = 0;
    static int[] a = new int[N];
    static int n, m;
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        for (int i = 0; i < n; i++) {
            m = sc.nextInt();
            if (m == 0) continue;
            int unStateNum = 0;
            for (int j = 0; j < m; j++) {
                // 谁不满足就加上他自己的位置
                unStateNum += (1 << sc.nextInt() - 1);
            }
            // 加自己，整个标志不满足的状态
            unStateNum += (1 << i);
            unState[unStateLen++] = unStateNum;
        }
        for (int i = 0; i < 1 << n; i++) {
            if (check(i)) {
                state[stateLen++] = i;
            }
        }
        int maxHappy = 0;

        for (int i = 0; i < stateLen; i++) {
            maxHappy = Math.max(maxHappy, cal(state[i]));
        }
        out.println(maxHappy);
        out.flush();
    }

    // 根据状态计算
    static int cal(int state) {
        int res = 0;
        for (int i = 0; i < n; i++) {
            // 移动到他自己的位置
            res += a[i] * (state >> i & 1);
        }
        return res;
    }

    static boolean check(int x) {
        for (int i = 0; i < unStateLen; i++) {
            if ((x & unState[i]) == unState[i]) {
                return false;
            }
        }

        return true;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
